<?php if (!defined('BASEPATH'))
    exit ('No direct script access allowed');
    /**
     *    会员
     * @author xiahuiqin
     *
     */
    class Person extends CI_Controller
    {
        public function  __construct()
        {
            parent::__construct();

            $this->load->model("member_mdl", 'm');
            $this->load->model("common_mdl", 'common');

            $this->id        = $this->session->userdata('member_id');
            $this->music     = $this->common->get_list(array('sign' => 'music')); //音乐
            $this->knowledge = $this->common->get_list(array('sign' => 'knowledge')); //教育背景
            $this->position  = $this->common->get_list(array('sign' => 'position')); //职位
            $this->business  = $this->common->get_list(array('sign' => 'business')); //行业

            if (!empty($this->id)) {
                $this->member = $this->m->get_one_list($this->id);
            } else {
                $url = base_url() . 'login/login';
                echo "<script>alert('您还未登录');window.location.href='" . $url . "';</script>";
                exit;
            }
        }

        /**
         * 个人中心欢迎首页
         */

        public function  index()
        {
            $this->person_data();
        }

        /**
         * 用户信息,兴趣爱好,修改头像，我的推荐
         */
        function person_data($id = '')
        {
            $ids               = $this->uri->segment(3);
            $data              = array();
            $data['member']    = $this->member;
            $data['knowledge'] = $this->knowledge;
            $data['position']  = $this->position;
            $data['business']  = $this->business;
            $data['music']     = $this->music;

            if ($this->input->post()) {
                if ($id == 1) {
                    $datas['name']       = $this->input->post('name'); //用户名
                    $datas['gender']     = $this->input->post('gender'); //性别，0女，1男
                    $datas['birth_date'] = $this->input->post('birth_date'); //出生日期
                    $datas['phone']      = $this->input->post('phone'); //联系电话
                    $datas['email']      = $this->input->post('email'); //邮箱

                    if (strlen($this->input->post('password')) == 32) {
                        $datas['password'] = $this->input->post('password'); //密码
                    } elseif (strlen($this->input->post('password')) == 0) {
                        $datas['password'] = $this->member['password'];
                    } else {
                        $datas['password'] = md5($this->input->post('password')); //密码
                    }
                } elseif ($id == 2) {
                    $datas['address']      = $this->input->post('address'); //地址
                    $datas['music_id']     = $this->input->post('music_id'); //音乐
                    $datas['knowledge_id'] = $this->input->post('knowledge_id'); //教育背景
                    $datas['position_id']  = $this->input->post('position_id'); //职位
                    $datas['business_id']  = $this->input->post('business_id'); //行业
                } elseif ($id == 3) {
                    $mem     = $this->m->get_one_list($this->id);
                    $img_url = './upload/avator/' . $mem['img'];
                    unlink($img_url); //删除上次的图

                    $data         = thumbnail('img', $_FILES['img'], './upload/avator/', 80, 80, TRUE, TRUE);
                    $datas['img'] = $data['thumbName'];
                } else {
                    exit('参数有误');
                }

                if ($this->m->update_byfield('id', $this->id, $datas)) {
                    $data['message'] = '编辑信息成功！';
                } else {
                    $data['message'] = '网络不给力，请稍后重试 ~！';
                }
                
                $data['member'] = $this->m->get_one_list($this->id);
                $this->load->view('member/personal_data_list' . $id, $data);
            } else {
                $this->load->view('member/personal_data_list' . $ids, $data);
            }

        }

        /**
         * 会员编辑
         */
        function member_edit()
        {
            $id = $this->uri->segment('4');

            if ($_POST) {
                $datas             = $this->_post_data();
                $datas['reg_time'] = $this->input->post('reg_time');
                $datas['img']      = $this->_upload_img('img', '1');

                if ($this->m->update_byfield('id', $id, $datas)) {
                    $data['error'] = "更新会员信息成功！";
                } else {
                    $data['error'] = '更新失败！';
                }
            } else {
                $data['error'] = '';
            }
            $data['one'] = $this->m->get_one_list($id);

            $this->layout->view('member/member_editV', $data);
        }

    }